package jack.handlerdemo;

import java.util.UUID;
import jack.handler.Handler;
import jack.handler.Looper;
import jack.handler.Message;

/**
 * created by Jack
 * email:yucrun@163.com
 * date: 2019/5/20
 * describe:
 */
class HandlerTest {
    public static void main(String[] args) {

        Looper.prepare();

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                System.out.println("Thread： " + Thread.currentThread().getName() + " received: " + msg);
            }
        };

        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(500);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Message message = new Message();
                        message.what = 1;
                        message.obj = UUID.randomUUID().toString();     //多线程情况下，加锁，防止数据乱，  没明白原因。。。
                        System.out.println("Thread： " + Thread.currentThread().getName() + " send: " + message);
                        handler.sendMessage(message);
                    }

                }
            }).start();
        }
        Looper.loop();
    }
}
